package com.zr.subclass.service.impl;

import com.zr.subclass.mapper.SubClassMapper;
import com.zr.subclass.pojo.*;
import com.zr.subclass.service.SubClassService;
import com.zr.utils.AllRecords;
import com.zr.utils.ResultBuilderVo;
import com.zr.utils.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Lenovo on 2019/12/22.
 */
@Service
public class SubClassServiceImpl implements SubClassService{
    @Autowired
    private SubClassMapper planClassMapper;

    @Override
    @Transactional
    public ResultVo addPlanClass(@Valid SubClassAddVo planClassAddVo) {
        //验证计划大类编码不能重复
        int countCode = planClassMapper.queryByCode(planClassAddVo.getCode());
        if (countCode > 0) {
            return ResultBuilderVo.error("计划大类编码已经存在!");
        }
        //验证名称
        ResultVo resultVo =  vlidate(planClassAddVo.getName());
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //给创建人员，时间等赋值
        SubClassVo planClassVo = new SubClassVo();
        //BeanUtils.copyProperties 可以把左边实体类的字段赋值给右边实体类的字段
        //注意：字段名必须一致才能赋值
        //注意：如果存在集合类型 集合类型必须一致，集合名称也必须一致
        BeanUtils.copyProperties(planClassAddVo, planClassVo);
        Date nowDate = new Date();
        planClassVo.setCreateName("小明");
        planClassVo.setCreateTime(nowDate);
        planClassVo.setUpdateName("小明");
        planClassVo.setUpdateTime(nowDate);
        //入库
        planClassMapper.add(planClassVo);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo updatePlanClass(@Valid SubClassUpdateVo planClassUpdateVo) {
        //验证修改的数据是否存在
        SubClassVo planClassVo = planClassMapper.queryById(planClassUpdateVo.getId());
        if (planClassVo==null){
            return ResultBuilderVo.error("修改数据不存在!");
        }
        if (!planClassVo.getStatus()){
            return ResultBuilderVo.error("禁用");
        }
        if (planClassVo.getStatus()){
            return ResultBuilderVo.error("启用");
        }
        //验证名称
        ResultVo resultVo =  vlidate(planClassUpdateVo.getName());
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //赋值updateName updateTime赋值
        SubClassVo planClass = new SubClassVo();
        //BeanUtils.copyProperties 可以把左边实体类的字段赋值给右边实体类的字段
        //注意：字段名必须一致才能赋值
        //注意：如果存在集合类型 集合类型必须一致，集合名称也必须一致
        BeanUtils.copyProperties(planClassUpdateVo, planClass);
        Date nowDate = new Date();
        planClassVo.setUpdateName("小明");
        planClassVo.setUpdateTime(nowDate);
        planClassMapper.update(planClass);
        return ResultBuilderVo.success();
    }

    @Override
    public ResultVo see(Integer id) {
        SubClassVo planClassVo = planClassMapper.queryById(id);
        if (planClassVo==null){
            return ResultBuilderVo.error("操作对象不存在");
        }
        return ResultBuilderVo.success(planClassVo);
    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid SubClassUpdateStatusVo planClassUpdateStatusVo) {
        SubClassVo planClassVo = planClassMapper.queryById(planClassUpdateStatusVo.getId());
        if (planClassVo==null){
            return ResultBuilderVo.error("操作对象不存在");
        }
        SubClassVo planClassVo1 = new SubClassVo();
        BeanUtils.copyProperties(planClassUpdateStatusVo,planClassVo1);
        Date nowDate = new Date();
        planClassVo.setUpdateName("小明");
        planClassVo.setUpdateTime(nowDate);
        planClassMapper.updateStatus(planClassVo1);
        return ResultBuilderVo.success();
    }

    @Override
    public ResultVo queryPage(SubClassSelectVo planClassSelectVo) {
        AllRecords allRecords = new AllRecords();
        //根据条件查询总条数
        int count =  planClassMapper.queryCount(planClassSelectVo);
        //根据条件查询数据
        List<SubClassVo> planClassVoList = planClassMapper.queryData(planClassSelectVo);
        for (SubClassVo planClassVo:planClassVoList){
            if (planClassVo.getStatus()!=null){
                if (!planClassVo.getStatus()){
                    planClassVo.setStatusName("禁用");
                }else {
                    planClassVo.setStatusName("启用");
                }
            }

        }
        allRecords.setPageIndex(planClassSelectVo.getPageIndex());
        allRecords.setPageSize(planClassSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.setDataList(planClassVoList);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }

    //验证名称不能重复
    private ResultVo vlidate(String name) {
        int countName = planClassMapper.queryByName(name);
        if (countName > 0) {
            return ResultBuilderVo.error("计划大类名称已经存在!");
        }
        return ResultBuilderVo.success();
    }
}
